package bank;

public class _04MidNumber {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        /*int length = nums1.length + nums2.length;
        int[] numAll = new int[length];
        for(int i = 0;i<nums1.length;i++){
            numAll[i] = nums1[i];
        }
        for(int i = 0;i<nums2.length;i++){
            numAll[i+nums1.length] = nums2[i];
        }
        Arrays.sort(numAll);
        */
        //以上方法效率较低
        int[] numAll = new int[nums1.length + nums2.length];
        int is1=0,is2=0;
        for(int i = 0;i<numAll.length;i++){
            if(is1 < nums1.length && is2 < nums2.length){
                if(nums1[is1] <= nums2[is2]){
                    numAll[i] = nums1[is1++];
                }else if(nums1[is1] > nums2[is2]){
                    numAll[i] = nums2[is2++];
                }
            }else if(is1 == nums1.length){
                //nums1中的元素添加完了，需要补充nums2中元素
                numAll[i] = nums2[is2++];
            }else if(is2 == nums2.length){
                numAll[i] = nums1[is1++];
            }

        }
        double mid;
        if(numAll.length % 2 == 0){
            mid = (numAll[numAll.length/2 - 1]+numAll[numAll.length/2])/2.0;
            return mid;
        }else{
            mid = numAll[numAll.length/2];
            return mid;
        }
    }
}
